/**
 * @author wxn
 * @since 12-5-24 1:24
 */
package com.sihai.ehr.web.action.organization;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.AuditType;
import com.sihai.ehr.model.biz.Auditing;
import com.sihai.ehr.model.biz.Duty;
import com.sihai.ehr.model.sys.AuditStatus;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.model.sys.OrganizationAuditing;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.Iterator;
import java.util.List;

@Results({@Result(name = "audit_list", location = "audit_list.jsp"),
          @Result(name = "audit_list_main", location = "main_tab.jsp"),
          @Result(name = "audit_detail", location = "audit_detail.jsp"),
          @Result(name = "audit", location = "audit_list.jsp")})
public class AuditAction extends EHRAction {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 7204414972672240877L;

    @BeanInject
    private OrganizationService organizationService;

    private List<Organization> topOrganizations; //顶级部门
    private LimitedList<Organization> organizations;
    private OrganizationAuditing auditing;
    private Organization organization; //当前审核组织机构对象

    private List<List<OrganizationAuditing>> audits;

    @Action("/organization/audit_list")
    @Override
    public String execute() throws Exception {
        topOrganizations = organizationService.findChildren(null, true, null, null); //顶级组织结构
        Organization organization = new Organization();
        if(organizationId == null || organizationId <= 0){
            if(topOrganizations != null && !topOrganizations.isEmpty()){
            	if(getCurrentUserId() > 0){
                	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
                	Iterator<Organization> it = organizations.iterator();
                	while(it.hasNext()){
                		Organization o = it.next();
                		if(o.getId() != topOrgId){
                			it.remove();
                		}
                	}
                }
                organizationId = topOrganizations.get(0).getId();
            }
        }
        organizations = organizationService.findForAudit(organizationId, this.createLimit(), this.createSorter());

        /* 分页 */
        this.paging(organizations);
        /* 设置翻页action */
        this.setPageUrl("organization/audit_list.xhtml");

        return "audit_list";
    }

    @Action("/organization/audit_list_main")
    public String main() throws Exception {
        listAction = "/organization/audit_list.xhtml";
        topOrganizations = organizationService.findChildren((Long)null, false, null, null);
        if(topOrganizations != null && !topOrganizations.isEmpty()){
            organizationId = topOrganizations.get(0).getId();
        }
        return "audit_list_main";
    }

    //显示审核详细页面
    @Action("/organization/audit_detail")
    public String detail() throws Exception {
        organization = organizationService.get(organizationId);
        audits = organizationService.findOrganizationAuditing(organizationId);
        if(audits == null || audits.isEmpty()){
            organizationService.saveOrganizationAuditingProcess(organizationId);
            audits = organizationService.findOrganizationAuditing(organizationId);
        }
        //检查当前登录用户是否可以审核当前结点
        if(audits != null && !audits.isEmpty()){
            checkCanAudit((Duty)null, audits.get(audits.size()-1));
        }
        currentAuditType = AuditType.audit_organization.toString();
        return "audit_detail";
    }

    public List<Organization> getTopOrganizations() {
        return topOrganizations;
    }

    public void setTopOrganizations(List<Organization> topOrganizations) {
        this.topOrganizations = topOrganizations;
    }

    public LimitedList<Organization> getOrganizations() {
        return organizations;
    }

    public void setOrganizations(LimitedList<Organization> organizations) {
        this.organizations = organizations;
    }

    public OrganizationAuditing getAuditing() {
        return auditing;
    }

    public void setAuditing(OrganizationAuditing auditing) {
        this.auditing = auditing;
    }

    public List<List<OrganizationAuditing>> getAudits() {
        return audits;
    }

    public void setAudits(List<List<OrganizationAuditing>> audits) {
        this.audits = audits;
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }
}
